package com.macbulldev.poc.testgin.domain;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class StateOrProvince {
	
	
	private static final Logger LOGGER = Logger.getLogger(StateOrProvince.class);
	
	@NotEmpty(message="Display Name cannot be empty")
	private String displayName;
	
	@NotEmpty(message="Code cannot be empty")
    private String code;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "stateOrProvince")
    private Set<Rate> rates;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "stateOrProvince")
    private Set<Scenario> scenarios;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "stateOrProvince")
    private Set<PolicyForm> policyForms;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "stateOrProvince")
    private Set<TransactionQuestion> transactionQuestions;

    private String createUsername;
    private String updateUsername;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    
    public static StateOrProvince getByCode(String code) {
    	
    	LOGGER.debug("Get By Code:  "+code);
    	EntityManager entityManager = new StateOrProvince().entityManager;
    	
    	String hql = "select sop from StateOrProvince sop where upper(trim(sop.code))=upper(trim(?))";
    	LOGGER.debug("hql:  "+hql);
    	
    	Query q = entityManager.createQuery(hql);
    	LOGGER.debug("Query:  "+q);
    	
    	q.setParameter(1, code);
        
    	StateOrProvince retObj = null;
    	
    	try{
    		retObj = (StateOrProvince)q.getSingleResult();
    	}catch(Exception ex)
    	{
    		LOGGER.debug("query error:  "+ex);
    	}
    	
        return retObj;
    }
    public static StateOrProvince getByDisplayName(String displayName) {
    	
    	LOGGER.debug("Get By Code:  "+displayName);
    	EntityManager entityManager = new StateOrProvince().entityManager;
    	
    	String hql = "select sop from StateOrProvince sop where upper(trim(sop.displayName))=upper(trim(?))";
    	LOGGER.debug("hql:  "+hql);
    	
    	Query q = entityManager.createQuery(hql);
    	LOGGER.debug("Query:  "+q);
    	
    	q.setParameter(1, displayName);
    	
    	StateOrProvince retObj = null;
    	
    	try{
    		retObj = (StateOrProvince)q.getSingleResult();
    	}catch(Exception ex)
    	{
    		LOGGER.debug("query error:  "+ex);
    	}
    	
        return retObj;
    }
    

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            StateOrProvince attached = this.entityManager.find(StateOrProvince.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        StateOrProvince merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new StateOrProvince().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countStateOrProvinces() {    
        return (Long) entityManager().createQuery("select count(o) from StateOrProvince o").getSingleResult();        
    }

	public static List<StateOrProvince> findAllStateOrProvinces() {    
        return entityManager().createQuery("select o from StateOrProvince o order by o.displayName asc").getResultList();        
    }

	public static StateOrProvince findStateOrProvince(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of StateOrProvince");        
        return entityManager().find(StateOrProvince.class, id);        
    }

	public static List<StateOrProvince> findStateOrProvinceEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from StateOrProvince o order by o.displayName asc").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public String getDisplayName() {    
        return this.displayName;        
    }

	public void setDisplayName(String displayName) {    
        this.displayName = displayName;        
    }

	public String getCode() {    
        return this.code;        
    }

	public void setCode(String code) {    
        this.code = code;        
    }

	public Set<Rate> getRates() {    
        return this.rates;        
    }

	public void setRates(Set<Rate> rates) {    
        this.rates = rates;        
    }

	public Set<Scenario> getScenarios() {    
        return this.scenarios;        
    }

	public void setScenarios(Set<Scenario> scenarios) {    
        this.scenarios = scenarios;        
    }

	public Set<PolicyForm> getPolicyForms() {    
        return this.policyForms;        
    }

	public void setPolicyForms(Set<PolicyForm> policyForms) {    
        this.policyForms = policyForms;        
    }

	public List<TransactionQuestion> getTransactionQuestions() {  
		
		EntityManager em = new TransactionQuestion().entityManager;
		return em.createQuery("select o from TransactionQuestion o where o.stateOrProvince.id = ? order by o.Type desc").setParameter(1, this.id).getResultList();
		
        //return this.transactionQuestions;        
    }

	public void setTransactionQuestions(Set<TransactionQuestion> transactionQuestions) {    
        this.transactionQuestions = transactionQuestions;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("DisplayName: ").append(getDisplayName()).append(", ");        
        sb.append("Code: ").append(getCode()).append(", ");        
        sb.append("Rates: ").append(getRates() == null ? "null" : getRates().size()).append(", ");        
        sb.append("Scenarios: ").append(getScenarios() == null ? "null" : getScenarios().size()).append(", ");        
        sb.append("PolicyForms: ").append(getPolicyForms() == null ? "null" : getPolicyForms().size()).append(", ");        
        sb.append("TransactionQuestions: ").append(getTransactionQuestions() == null ? "null" : getTransactionQuestions().size()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        return sb.toString();        
    }
}
